package cn.shop.dys.util;

import android.content.Context;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.db.table.DbModel;
import com.lidroid.xutils.exception.DbException;
import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.bean.Msg;

/**
 * Created by jian on 2016/4/10.
 */
public class MyDBUtil {

    private static DbUtils dbUtils;

    public static void initDB(Context context,String dbName){
        dbUtils = DbUtils.create(context, dbName);
    }
    public static void saveMsg(Msg msg){
        try {
            dbUtils.save(msg);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static void saveComeMsg(Msg msg){
        try {
            Msg saveMsg = dbUtils.findFirst(Selector.from(Msg.class).where("jid", "=", msg.jid).orderBy("time", true));
            if(saveMsg!=null){
                if(saveMsg.msgCount==0){
                    msg.msgCount=1;
                }else {
                    msg.msgCount=saveMsg.msgCount+1;
                }
            }else {
                msg.msgCount=1;
            }
            dbUtils.save(msg);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static void clearNewMsgCountByJid(String jid){
        try {
            Msg saveMsg = dbUtils.findFirst(Selector.from(Msg.class).where("jid", "=", jid).orderBy("time", true));
            if(saveMsg==null)return;
            saveMsg.msgCount=0;
            WhereBuilder whereBuilder = WhereBuilder.b("jid", "=", jid);
            dbUtils.update(saveMsg,whereBuilder,new String[]{"msgCount"});
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static List<Msg> querySingleByJid(String jid){
        List<Msg> msgInfos=null;
        try {
            msgInfos = dbUtils.findAll(Selector.from(Msg.class).where("jid", "=", jid));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return msgInfos;
    }
    public static List<Msg> queryAllGroupByJid(){
        List<Msg> msgInfos=null;
        try {
            List<DbModel> dbModelAll = dbUtils.findDbModelAll(Selector.from(Msg.class)
                    .groupBy("jid").orderBy("time", true)
                    .select(new String[]{"jid", "content", "type","msgCount"}));
            if(dbModelAll==null) return null;
            msgInfos=new ArrayList<>();
            for (DbModel dbModel : dbModelAll) {
                Msg msg = Msg.creatMsg(
                        dbModel.getString("jid"),
                        dbModel.getString("content"),
                        dbModel.getInt("type"));
                msg.msgCount=dbModel.getInt("msgCount");
                msgInfos.add(msg);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return msgInfos;
    }
    public static void deleteMsgByJid(String jid){
        WhereBuilder whereBuilder = WhereBuilder.b("jid", "=", jid);
        try {
            dbUtils.delete(Msg.class,whereBuilder );
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
